<?php
##############################################
#
#   购物车
# @ Time : 2017-2-26 - 下午11:32:05
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Think\Controller;
use Home\Controller\Cart;
use Home\Model\CartModel;

class CartController extends Controller{
	protected $_cartObject;
	private $_uid = null;
	
	
	public function _initialize(){
		$this -> _cartObject  = new CartModel();
		if($_SESSION['uid']){
			$this -> _uid = $_SESSION['uid'];
			//将session的数据写入到购物车
			$this -> writeCart();
		}
	}
	
	//显示购物车页
	public function index(){
		$cart = $this -> getCartData();
		$data = $this -> disCart($cart);
		$this -> assign('cart', $data[0]);
		$this -> assign('total', $data[1]);
		dump($data);
		$this -> display('index');
	}
	
	//获取购物车数据
	private function getCartData(){
		//用户未登入
		if(is_null($this -> _uid)){
			if(!isset($_SESSION['cart']['goods'])) return;
			$carts = $_SESSION['cart']['goods'];
			$result = array();
			foreach ($carts as $k => $v){
				$data = $this -> _cartObject -> getGoods(array('goods_id' => $v['id']));
				$data['goods_num'] = $v['num'];
				$result[] = $data;
			}
		//用户已登入
		}else{
			
		}
		return $result;
	}
	
	/**
	 * 处理购物车数据
	 * @param array $cart
	 * @return array
	 */
	public function disCart($cart){
		if(empty($cart)) return;
		$total = 0;
		foreach ($cart as $k => $v){
			$cart[$k]['xiaoji'] = sprintf("%.2f",$v['goods_num'] * $v['price']);
			$cart[$k]['status'] = $v['end_time'] < time() ? '已下架' : '可购买';
			$total +=  $cart[$k]['xiaoji'];
		}
		return array($cart, sprintf("%.2f",$total));
	}
	
	//添加到购物车
	public function add(){
		if(!IS_AJAX) return;
		//用户未登入
		if(is_null($this -> _uid)){
			$data = array(
				'id' => intval(I('get.gid')),
				'name' => '',
				'num' => 1,
				'price' => 0
			);
			Cart::add($data);
			$total = count($_SESSION['cart']['goods']);
			$result = array('status' => true, 'total' => $total);
		//用户已登入
		}else{
			$data = array(
				'goods_id' => intval(I('get.gid')),
				'user_id' => $this -> _uid,
				'goods_num' => 1
			);
			$result = $this -> checkAdd($data);
			if($result){
				$where = array('user_id' => $data['user_id']);
				$total = $this -> _cartObject -> countCart();
				$result = array('status' => true, 'total' => $total);
			}
		}
		$this -> ajaxReturn($result);
	}
	
	//将session的数据写入到购物车
	private function writeCart(){
		if(!isset($_SESSION['cart']['goods'])) return;
		$carts = $_SESSION['cart']['goods'];
		$data = array();
		foreach ($carts as $k => $v){
			$data['user_id'] = $this -> _uid;
			$data['goods_id'] = $v['id'];
			$data['goods_num'] = $v['num'];
			$this -> checkAdd($data);
		}
		unset($_SESSION['cart']);
	}
	
	/**
	 * 验证添加
	 * 存在自增，不存在添加新数据
	 * @param array $data
	 */
	public function checkAdd($data){
		$where = array('user_id' => $data['user_id'], 'goods_id' => $data['goods_id']);
		$cart_id = $this -> _cartObject -> checkCart($where);
		if($cart_id){
			return $this -> _cartObject -> incCart($cart_id, $data['goods_num']);
		}else{
			return $this -> _cartObject -> addCart($data);
		}
	}
	
}